{**************************************************************************************************}
{ The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); }
{ you may not use this file except in compliance with the License. You may obtain a copy of the    }
{ License at http://www.mozilla.org/MPL/                                                           }
{                                                                                                  }
{ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF   }
{ ANY KIND, either express or implied. See the License for the specific language governing rights  }
{ and limitations under the License.                                                               }
{                                                                                                  }
{ The Original Code is ConversionUtils.pas                                                         }
{                                                                                                  }
{ The Initial Developer of the Original Code is Fabrizio Vita (fabrizio.vita@itacasoft.com)        }
{**************************************************************************************************}
unit ConversionUtils;

interface

uses
  {$IFDEF MSWINDOWS}
  Windows,
  {$ENDIF MSWINDOWS}
  Classes, ImageHlp;

type
  TLoadedImage = _LOADED_IMAGE;

function StrToBoolean(const S: string): Boolean;
function IntToBool(I: Integer): Boolean;
function BoolToInt(B: Boolean): Integer;
function GetModulePath: string;
function StringToPAnsiChar(stringVar : string) : PAnsiChar;
function VariantToString(Value: Variant): String;


implementation

uses sysutils,
{$IFDEF IS_CGI}
   forms,
{$ENDIF}
variants;

const
  DefaultTrueBoolStr  = 'True';  // DO NOT LOCALIZE
  DefaultFalseBoolStr = 'False'; // DO NOT LOCALIZE

  DefaultYesBoolStr   = 'Yes';   // DO NOT LOCALIZE
  DefaultNoBoolStr    = 'No';    // DO NOT LOCALIZE

function StrToBoolean(const S: string): Boolean;
var
  LowerCasedText: string;
begin
  { TODO : Possibility to add localized strings, like in Delphi 7 }
  { TODO : Lower case constants }
  LowerCasedText := LowerCase(S);
  Result := ((S = '1') or
    (LowerCasedText = LowerCase(DefaultTrueBoolStr)) or (LowerCasedText = LowerCase(DefaultYesBoolStr))) or
    (LowerCasedText = LowerCase(DefaultTrueBoolStr[1])) or (LowerCasedText = LowerCase(DefaultYesBoolStr[1]));
  if not Result then
  begin
    Result := not ((S = '0') or
      (LowerCasedText = LowerCase(DefaultFalseBoolStr)) or (LowerCasedText = LowerCase(DefaultNoBoolStr)) or
      (LowerCasedText = LowerCase(DefaultFalseBoolStr[1])) or (LowerCasedText = LowerCase(DefaultNoBoolStr[1])));
    if Result then
      raise Exception.Create('Error converting string to boolean');
  end;
end;

function IntToBool(I: Integer): Boolean;
begin
  Result := I <> 0;
end;

function BoolToInt(B: Boolean): Integer;
begin
  Result := Ord(B);
end;

///Restituisce il path completo di nome della dll corrente
function GetModulePath: string;
var
   DLLFileName: array[0..MAX_PATH] of Char;
   i: integer;
begin
{$IFDEF IS_CGI}
   result := Application.ExeName;
{$ELSE}
  {$IFDEF UNICODE}
     GetModuleFileNameW(hInstance, DLLFileName, MAX_PATH);
  {$ELSE}
     GetModuleFileName(hInstance, DLLFileName, MAX_PATH);
  {$ENDIF}
   result := string(DLLFileName);
{$ENDIF}

   i := Pos('\\?\', result);
   if i = 1 then
   begin
      result := copy(result, 5, length(result));
   end;

end;

function LinkerTimeStamp(const FileName: string): TDateTime;
var
  LI: TLoadedImage;
begin
{$WARNINGS OFF}
  {$IFDEF UNICODE}
  Win32Check(MapAndLoad(PAnsiChar(AnsiString(FileName)), nil, @LI, False, True));
  {$ELSE}
  Win32Check(MapAndLoad(PChar(FileName), nil, @LI, False, True));
  {$ENDIF}
{$WARNINGS ON}
  Result := LI.FileHeader.FileHeader.TimeDateStamp / SecsPerDay + UnixDateDelta;
  UnMapAndLoad(@LI);
end;


function StringToPAnsiChar(stringVar : string) : PAnsiChar;
Var
  AnsString : AnsiString;
  InternalError : Boolean;
begin
  InternalError := false;
  Result := '';
  try
    if stringVar <> '' Then
    begin
       AnsString := AnsiString(StringVar);
       Result := PAnsiChar(PAnsiString(AnsString));
    end;
  Except
    InternalError := true;
  end;
  if InternalError or (String(Result) <> stringVar) then
  begin
    Raise Exception.Create('Conversion from string to PAnsiChar failed!');
  end;
end;

function VariantToString(Value: Variant): String;
var
   f: TFormatSettings;
begin
   if VarIsNull(Value) then
   begin
    result := '';
    exit;
   end;

   f.DecimalSeparator := '.';


   case TVarData(Value).VType of
      varShortInt,
      varByte,
      varWord,
      varLongWord,
      varInt64,
      varUInt64,
      varSmallInt,
      varInteger   : Result := IntToStr(Value);
       varSingle,
      varDouble,
      varCurrency  : Result := FloatToStr(Value, f);
      varDate      : Result := FormatDateTime('dd/mm/yyyy', Value);
      varBoolean   : if Value then Result := 'true' else Result := 'false';
      varString, varUString : Result := Value;
    else           Result := '';
  end;
end;


end.
